package cn.edu.cjw.APMFP.ui.dialog;

import cn.edu.cjw.APMFP.BOMTable.Service.BTService;
import cn.edu.cjw.APMFP.BOMTable.Service.BTServiceImpl;
import cn.edu.cjw.APMFP.BOMTable.pojo.BOMTable;
import cn.edu.cjw.APMFP.Util.StringUtil;
import cn.edu.cjw.APMFP.ui.table.TableCallBack;

import javax.swing.*;
import java.awt.*;


public class BomDialog extends JDialog {

	private JButton confirmBtn = new JButton("确认");
	private JButton cancelBtn = new JButton("取消");

	private BTService service = new BTServiceImpl();

	private BOMTable entity;

	private TableCallBack callBack;

	public BomDialog(BOMTable entity, TableCallBack callBack){
		this.entity = entity;
		this.callBack = callBack;
		this.setTitle(entity != null ? "编辑" : "新增");
		this.setSize(500, 500);
		this.init();
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
//		this.pack();
		this.setVisible(true);
	}

	private void init(){
		this.setLayout(new BorderLayout());

		GridBagLayout gridBagLayout = new GridBagLayout();
		JPanel centerPanel = new JPanel(gridBagLayout);
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets(10, 10, 10, 10);
		constraints.ipady = 10;

		JLabel idLabel = new JLabel("编号:");
		JTextField idField = new JTextField(27);
		constraints.gridx = 0;
		constraints.gridy = 0;
		centerPanel.add(idLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(idField, constraints);

		JLabel nameLabel = new JLabel("名称:");
		JTextField nameField = new JTextField(27);
		constraints.gridx = 0;
		constraints.gridy = 1;
		centerPanel.add(nameLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(nameField, constraints);

		JLabel typeLabel = new JLabel("类型:");
		JComboBox<String> typeField = new JComboBox<>();
		typeField.addItem("成品");
		typeField.addItem("原料");
		typeField.setPreferredSize(nameField.getPreferredSize());
		constraints.gridx = 0;
		constraints.gridy = 2;
		centerPanel.add(typeLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(typeField, constraints);

		JLabel unitLabel = new JLabel("单位:");
		JTextField unitField = new JTextField(27);
		constraints.gridx = 0;
		constraints.gridy = 3;
		centerPanel.add(unitLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(unitField, constraints);

		this.add(centerPanel, BorderLayout.CENTER);

		if(entity != null){
			idField.setText(entity.getBId());
			nameField.setText(entity.getBName());
			typeField.setSelectedItem(entity.getBtype());
			unitField.setText(entity.getUnit());
		}

		JPanel footerPanel = new JPanel();
		footerPanel.setPreferredSize(new Dimension(this.getWidth(), 100));
		footerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 10));
		footerPanel.add(confirmBtn);
		footerPanel.add(cancelBtn);
		confirmBtn.addActionListener(e -> {
			String id = idField.getText().trim();
			if(!StringUtil.isNotBlank(id)){
				JOptionPane.showMessageDialog(null, "id不能为空");
				return;
			}
			String name = nameField.getText().trim();
			if(!StringUtil.isNotBlank(name)){
				JOptionPane.showMessageDialog(null, "name不能为空");
				return;
			}
			Object type = typeField.getSelectedItem();
			if(type == null){
				JOptionPane.showMessageDialog(null, "类型不能为空");
				return;
			}
			String unit = unitField.getText().trim();
			if(!StringUtil.isNotBlank(unit)){
				JOptionPane.showMessageDialog(null, "unit不能为空");
				return;
			}
			try {
				if(entity == null){
					service.addBOM(id, name, type.toString(), unit);
				}else{
					service.updateBOM(entity, name, type.toString(), unit);
				}
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(null, "操作失败:" + ex.getMessage());
				return;
			}
			if(callBack != null){
				callBack.refreshTable();
			}
			JOptionPane.showMessageDialog(null, "操作成功");
			close();
		});
		cancelBtn.addActionListener(e -> {
			close();
		});
		this.add(footerPanel, BorderLayout.SOUTH);
	}
	
	private void close(){
		this.dispose();
	}
}
